﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using Common.Logging;
using System.Data.SqlClient;
using OpenPop.Mime;
using System.IO;
using System.Net.Mail;
using System.Net;

namespace EmailSchedule
{
    public class sendEmailCustomerJob : IJob
    {
        private static ILog _log = LogManager.GetLogger(typeof(sendEmailCustomerJob));
        public virtual void Execute(IJobExecutionContext context)
        {
            _log.Info(string.Format("sendEmailCustomerJob! - {0}", System.DateTime.Now.ToString("r")));
            try
            {
                if (Helper.PendingMessageQueue.Count > 0)
                {
                    MessageInfo messageQueue = Helper.PendingMessageQueue.Dequeue();
                    //_log.Info(string.Format("Hello World! - {0}", System.DateTime.Now.ToString("r")));
                         
                        using (var messageSend = new MailMessage(messageQueue.ToEmail, messageQueue.FromEmail))
                        {
                              try
                            {   
                           Helper.LogToFile(LogFileType.MESSAGE," #"+messageQueue.MessageId+"#"+messageQueue.Subject+"#"+messageQueue.FromEmail+"#"+messageQueue.ToEmail,Helper.sendEmailCustomer);
                            StringBuilder builder = new StringBuilder();
                            builder.Append("Chào " + messageQueue.FromName);
                            builder.AppendLine();
                            builder.Append("Bạn vừa gửi hồ sơ ứng tuyển cho vị trí Developer tại công ty ");
                            builder.AppendLine();
                            builder.Append("Chúc " + messageQueue.FromName + " thành công trong sự nghiệp và cuộc sống!");
                            builder.AppendLine();
                            builder.Append("Đây là email tự động, vui lòng không trả lời (reply) email này.");
                            builder.AppendLine();
                            builder.Append("Thân mến");
                            builder.AppendLine();
                            builder.Append("Phòng Dịch Vụ Khách Hàng");
                            builder.AppendLine();
                            messageSend.Subject = messageQueue.Subject;
                            messageSend.Body = builder.ToString();
                            using (SmtpClient client1 = new SmtpClient
                            {
                                EnableSsl = true,
                                Host = "smtp.gmail.com",
                                Port = 587,
                                Credentials = new NetworkCredential(messageQueue.ToEmail, Helper.emailServerPassword)
                            })
                            {
                                client1.Send(messageSend);
                                Helper.RespondingMessageQueue.Enqueue(messageQueue);
                            }
                           }
                              catch (Exception ex)
                              {
                                  Helper.MessageIdList.Remove(messageQueue.MessageId);
                                  Helper.LogToFile(LogFileType.EXCEPTION, "- sendEmailCustomerJob 1-" + ex.Message,Helper.sendEmailCustomer);
                              }
                        }                  
                }
            }
            catch (Exception ex)
            {
                   Helper.LogToFile(LogFileType.EXCEPTION, "- sendEmailCustomerJob -" + ex.Message,Helper.sendEmailCustomer);
            }
        }
    }
}
